package cn.softgame.cms.secure.nm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.softgame.cms.common.page.domain.PageResultBean;
import cn.softgame.cms.open.common.service.CommonService;
import cn.softgame.cms.secure.dictionary.domain.DictionaryItem;
import cn.softgame.cms.secure.dictionary.service.DictionaryService;
import cn.softgame.cms.secure.nm.domain.ResDevModel;
import cn.softgame.cms.secure.nm.domain.ResDevTemplate;
import cn.softgame.cms.secure.nm.service.NmService;

/**
 * 网管资源配置控制器
 * @author xuquan
 * 2013-3-27
 */
@Controller
@RequestMapping("/secure/nm/*")
public class NmController {
	
	@Autowired
	private NmService nmService;
	
	@Autowired
	private CommonService commonService;
	
	@Autowired
	private DictionaryService dictionaryService;
	
	@RequestMapping(value="init.html")
	public ModelAndView init(){
		ModelAndView mav = new ModelAndView();
		List<DictionaryItem> mfrsItemList = dictionaryService.qryDictionaryItemByDictCode("DICT_MFRS");
		List<DictionaryItem> neTypeList = dictionaryService.qryDictionaryItemByDictCode("DICT_NE_TYPE");
		mav.addObject("devModel",new ResDevModel());
		mav.addObject("mfrsItemList",mfrsItemList);
		mav.addObject("neTypeList",neTypeList);
		mav.setViewName("secure/nm/devModelForm");
		return mav;
	}
	
	/**
	 * 设备型号列表
	 * @return
	 */
	@RequestMapping(value="listDevModel.html")
	public ModelAndView listDevModel() {
		ModelAndView mav = new ModelAndView();
		List<DictionaryItem> neTypeList = dictionaryService.qryDictionaryItemByDictCode("DICT_NE_TYPE");
		mav.addObject("neTypeList",neTypeList);
		mav.addObject("condition",new ResDevModel());
		mav.addObject("devModel",new ResDevModel());
		mav.setViewName("secure/nm/listDevModel");
		return mav;
	}

	@RequestMapping(value="loadAllDevModels.html")
	public @ResponseBody PageResultBean loadAllDevModels(Integer jtStartIndex,Integer jtPageSize,String typeId,String modelName){
		PageResultBean pr = new PageResultBean();
		try {
			ResDevModel model = new ResDevModel();
			model.setTypeId(typeId);
			model.setModelName(modelName);
			model.setFromNumber(jtStartIndex);
			model.setToNumber(jtStartIndex+jtPageSize);
			pr.setResult("OK");
			pr.setRecords(nmService.qryAllDevModels(model));
			pr.setTotalRecordCount(nmService.qryTotalRecords(model));
		} catch (Exception e) {
			e.printStackTrace();
			pr.setResult("ERROR");
		}
		
		return pr;
	}
	
	@RequestMapping(value="save.html")
	public @ResponseBody PageResultBean save(ResDevModel devModel){
		PageResultBean pr = new PageResultBean();
		try {
			if(devModel!=null){
				String modelId = devModel.getModelId();
				if(modelId!=null && !"".equals(modelId)){
					nmService.update(devModel);
				}else{
					devModel.setModelId(commonService.getCode());
					nmService.save(devModel);
				}
			}
			pr.setResult("OK");
			pr.setRecord(devModel);
		} catch (Exception e) {
			e.printStackTrace();
			pr.setResult("ERROR");
			pr.setMessage("保存失败!");
		}
		return pr;
	}
	
	@RequestMapping(value="delete.html")
	public @ResponseBody PageResultBean delete(String modelId){
		PageResultBean pr = new PageResultBean();
		try {
			nmService.delete(modelId);
			pr.setResult("OK");
		} catch (Exception e) {
			e.printStackTrace();
			pr.setResult("ERROR");
			pr.setMessage("删除失败!");
		}
		return pr;
	}
	
	@RequestMapping(value="preUpdate.html")
	public ModelAndView preUpdate(String modelId){
		ModelAndView mav = new ModelAndView();
		List<DictionaryItem> mfrsItemList = dictionaryService.qryDictionaryItemByDictCode("DICT_MFRS");
		List<DictionaryItem> neTypeList = dictionaryService.qryDictionaryItemByDictCode("DICT_NE_TYPE");
		mav.addObject("devModel",nmService.qryDevModelByModelId(modelId));
		mav.addObject("mfrsItemList",mfrsItemList);
		mav.addObject("neTypeList",neTypeList);
		mav.setViewName("secure/nm/devModelForm");
		return mav;
	}
	
	@RequestMapping(value="initDevTemplate.html")
	public ModelAndView initDevTemplate(String modelId){
		ModelAndView mav = new ModelAndView();
		ResDevTemplate template = new ResDevTemplate();
		template.setModelId(modelId);
		mav.addObject("devTemplate",template);
		mav.setViewName("secure/nm/devTemplateForm");
		return mav;
	}
	
	/**
	 * 设备型号列表
	 * @return
	 */
	@RequestMapping(value="listDevTemplate.html")
	public ModelAndView listDevTemplate() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("secure/nm/listDevTemplate");
		return mav;
	}

	@RequestMapping(value="loadDevTemplates.html")
	public @ResponseBody PageResultBean loadDevTemplates(Integer jtStartIndex,Integer jtPageSize,String modelId){
		PageResultBean pr = new PageResultBean();
		try {
			ResDevTemplate template = new ResDevTemplate();
			template.setModelId(modelId);
			template.setFromNumber(jtStartIndex);
			template.setToNumber(jtStartIndex+jtPageSize);
			pr.setResult("OK");
			pr.setRecords(nmService.qryDevTemplates(template));
			pr.setTotalRecordCount(nmService.qryDevTemplateTotalRecords(template));
		} catch (Exception e) {
			e.printStackTrace();
			pr.setResult("ERROR");
		}
		
		return pr;
	}
	
	@RequestMapping(value="saveDevTemplate.html")
	public @ResponseBody PageResultBean saveDevTemplate(ResDevTemplate template){
		PageResultBean pr = new PageResultBean();
		try {
			if(template!=null){
				String templateId = template.getTemplateId();
				if(templateId!=null && !"".equals(templateId)){
					nmService.updateDevTemplate(template);
				}else{
					template.setTemplateId(commonService.getCode());
					nmService.saveDevTemplate(template);
				}
			}
			pr.setResult("OK");
			pr.setRecord(template);
		} catch (Exception e) {
			e.printStackTrace();
			pr.setResult("ERROR");
			pr.setMessage("保存失败!");
		}
		return pr;
	}
	
	@RequestMapping(value="deleteDevTemplate.html")
	public @ResponseBody PageResultBean deleteDevTemplate(String templateId){
		PageResultBean pr = new PageResultBean();
		try {
			nmService.deleteDevTemplate(templateId);
			pr.setResult("OK");
		} catch (Exception e) {
			e.printStackTrace();
			pr.setResult("ERROR");
			pr.setMessage("删除失败!");
		}
		return pr;
	}
	
	@RequestMapping(value="preUpdateDevTemplate.html")
	public ModelAndView preUpdateDevTemplate(String templateId){
		ModelAndView mav = new ModelAndView();
		mav.addObject("devTemplate",nmService.qryDevTemplateByTemplateId(templateId));
		mav.setViewName("secure/nm/devTemplateForm");
		return mav;
	}
}
